package cc.fs.myproject.utils;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by nekocode on 2015/4/6 0006.
 */
public class RecyclerViewFooter {
    private RecyclerView recyclerView;
    private boolean isLoadingMore = false;
    private AdapterFooterManager adapterFooterManager;
    private Runnable loadingMoreCallback;

    public static RecyclerViewFooter create(RecyclerView recyclerView, Runnable loadingMoreCallback) {
        if(recyclerView != null && recyclerView.getAdapter() != null) {
            RecyclerViewFooter recyclerViewFooter = new RecyclerViewFooter(recyclerView);
            recyclerViewFooter.setLoadingMoreCallback(loadingMoreCallback);
            recyclerViewFooter.setLoadingMore(true);
            return recyclerViewFooter;
        }

        return null;
    }

    private RecyclerViewFooter() {
    }

    private RecyclerViewFooter(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        recyclerView.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
            @Override
            public void onChildViewAdded(View parent, View child) {
                RecyclerView recyclerView = (RecyclerView) parent;
                int lastVisibleItem = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition();
                int totalItemCount = recyclerView.getAdapter().getItemCount();
                if (lastVisibleItem >= totalItemCount - 2 ) {   //&& dy > 0
                    if (isLoadingMore) {
                        loadingMoreCallback.run();
                    }
                }
            }

            @Override
            public void onChildViewRemoved(View parent, View child) {

            }
        });

        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItem = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition();
                int totalItemCount = recyclerView.getAdapter().getItemCount();
                if (lastVisibleItem >= totalItemCount - 2 && dy > 0) {
                    if (isLoadingMore) {
                        loadingMoreCallback.run();
                    }
                }
            }
        });
    }

    public void setLoadingMoreCallback(Runnable callback) {
        loadingMoreCallback = callback;
    }

    public void setLoadingMore(boolean loadingMore) {
        isLoadingMore = loadingMore;
        if(recyclerView.getAdapter() instanceof AdapterFooterManager) {
            ((AdapterFooterManager) recyclerView.getAdapter()).setLoadingMore(loadingMore);
        }
    }

    public interface AdapterFooterManager {
        public void setLoadingMore(boolean loadingMore);
    }
}
